class AccountsController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem
  
  # render new.rhtml
  def new
    @account = Account.new
  end

  def create
    unless simple_captcha_valid?
      flash.now[:notice] = "验证码错误！"
      redirect_to signup_path
      return
    end

    cookies.delete :auth_token
    # protects against session fixation attacks, wreaks havoc with 
    # request forgery protection.
    # uncomment at your own risk
    # reset_session
    @account = Account.new(params[:account])
    @account.save
    @user = User.new
    @user.nickname = @account.login
    @user.is_freeze = 0
    @account.user = @user
    if @account.errors.empty?
      self.current_account = @account
      redirect_back_or_default(user_path current_user)
      flash.now[:notice] = "Thanks for signing up!"
    else
      render :action => 'new'
    end
  end

  #验证用户名是否存在
  def valid_login_exist
    if Account.valid_login_exist? params[:account][:login]
      render :text => "false"
      return
    else
      render :text => "true"
      return
    end
  end

  #验证邮箱是否存在
  def valid_email_exist
    if Account.valid_email_exist? params[:account][:email]
      render :text => "false"
      return
    else
      render :text => "true"
      return
    end
  end
end
